import { HYEventStore } from "hy-event-store"
import {getTopList,getRankings,songDetail,getSongDetail} from "../service/api_music"
import {parseLyric} from "../utils/formatLyric"

const audioContext = wx.createInnerAudioContext()

const store = new HYEventStore({
    state:{
         hotList:{},
         rankingMap:{ 0: "newRanking", 1: "hotRanking", 2: "originRanking", 3: "upRanking" },
         newRanking: {}, // 0: 新歌
         hotRanking: {}, // 1: 热门
         originRanking: {}, // 2: 原创
         upRanking: {}, // 3: 飙升

         isFirstPlay: true,
         isStoping: false,

      id: 0,
      currentSong: {},
      durationTime: 0,
      lyricInfos: [],

      currentTime: 0,
      currentLyricIndex: 0,
      currentLyricText: "",

      isPlaying: false,

      playModeIndex: 0, // 0: 循环播放 1: 单曲循环 2: 随机播放
      playListSongs: [],
      playListIndex: 0
        
    },
    actions:{
      getTopListAction(ctx){
         getTopList(1).then((res)=>{
          //  console.log(res)
            ctx.hotList = res.playlist
        })
      
        //  state.playList = result.playList
      },
      getRankingsAction(ctx){
         for(let i=0;i<4;i++){
         
           getRankings(i).then((res)=>{
            // console.log(res)
            const map = ctx.rankingMap[i]
           // console.log(map)
            ctx[map] = res.playlist
           }) 
         }
      },
      playMusicWithSongIdAction(ctx, { id, isRefresh = false }) {
        if (ctx.id == id && !isRefresh) {
          this.dispatch("changeMusicPlayStatusAction", true)
          return
        }
        ctx.id = id
  
        // 0.修改播放的状态
        ctx.isPlaying = true
        ctx.currentSong = {}
        ctx.durationTime = 0
        ctx.lyricInfos = []
        ctx.currentTime = 0
        ctx.currentLyricIndex = 0
        ctx.currentLyricText = ""
  
        // 1.根据id请求数据
        // 请求歌曲详情
        getSongDetail(id).then(res => {
          console.log(res)
          ctx.currentSong = res.songs[0]
          ctx.durationTime = res.songs[0].dt
          audioContext.title = res.songs[0].name
        })
        // 请求歌词数据
        songDetail(id).then(res => {
          const lyricString = res.lrc.lyric
          const lyrics = parseLyric(lyricString)
          ctx.lyricInfos = lyrics
        })
  
        // 2.播放对应id的歌曲
        audioContext.stop()
        audioContext.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`
        audioContext.title = id
        audioContext.autoplay = true
  
        // 3.监听audioContext一些事件
        if (ctx.isFirstPlay) {
          this.dispatch("setupAudioContextListenerAction")
          ctx.isFirstPlay = false
        } 
      },
  
      setupAudioContextListenerAction(ctx) {
        // 1.监听歌曲可以播放
        audioContext.onCanplay(() => {
          audioContext.play()
        })
    
        // 2.监听时间改变
        audioContext.onTimeUpdate(() => {
          // 1.获取当前时间
          const currentTime = audioContext.currentTime * 1000
    
          // 2.根据当前时间修改currentTime
          ctx.currentTime = currentTime
          
          // 3.根据当前时间去查找播放的歌词
          if (!ctx.lyricInfos.length) return
          let i = 0
          for (; i < ctx.lyricInfos.length; i++) {
            const lyricInfo = ctx.lyricInfos[i]
            if (currentTime < lyricInfo.time) {
              break
            }
          }
          // 设置当前歌词的索引和内容
          const currentIndex = i - 1
          if (ctx.currentLyricIndex !== currentIndex) {
            const currentLyricInfo = ctx.lyricInfos[currentIndex]
            ctx.currentLyricIndex = currentIndex
            ctx.currentLyricText = currentLyricInfo.text
          }
        })
  
        // 3.监听歌曲播放完成
        audioContext.onEnded(() => {
          this.dispatch("changeNewMusicAction")
        })
  
        // 4.监听音乐暂停/播放/停止
        // 播放状态
        audioContext.onPlay(() => {
          ctx.isPlaying = true
        })
        // 暂停状态
        audioContext.onPause(() => {
          ctx.isPlaying = false
        })
        audioContext.onStop(() => {
          ctx.isPlaying = false
          ctx.isStoping = true
        })
      },
  
      changeMusicPlayStatusAction(ctx, isPlaying = true) {
        ctx.isPlaying = isPlaying
        if (ctx.isPlaying && ctx.isStoping) {
          audioContext.src = `https://music.163.com/song/media/outer/url?id=${ctx.id}.mp3`
          audioContext.title = currentSong.name
          audioContext.startTime = ctx.currentTime / 1000
          ctx.isStoping = false
        }
        ctx.isPlaying ? audioContext.play(): audioContext.pause()
      },
  
      changeNewMusicAction(ctx, isNext = true) {
        // 1.获取当前索引
        let index = ctx.playListIndex
  
        // 2.根据不同的播放模式, 获取下一首歌的索引
        switch(ctx.playModeIndex) {
          case 0: // 顺序播放
            index = isNext ? index + 1: index -1
            if (index === -1) index = ctx.playListSongs.length - 1
            if (index === ctx.playListSongs.length) index = 0
            break
          case 1: // 单曲循环
            break
          case 2: // 随机播放
            index = Math.floor(Math.random() * ctx.playListSongs.length)
            break
        }
  
        console.log(index)
  
        // 3.获取歌曲
        let currentSong = ctx.playListSongs[index]
        if (!currentSong) {
          currentSong = ctx.currentSong
        } else {
          // 记录最新的索引
          ctx.playListIndex = index
        }
  
        // 4.播放新的歌曲
        this.dispatch("playMusicWithSongIdAction", { id: currentSong.id, isRefresh: true })
      }
    }
}) 
export {audioContext ,store} 